package org.dromara.pe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.pe.domain.bo.TaskArrangeInfoBo;
import org.dromara.pe.domain.vo.AskLeaveVo;
import org.dromara.pe.domain.vo.TaskArrangeInfoVo;
import org.dromara.pe.service.ITaskArrangeInfoService;
import org.dromara.run.domain.RunList;
import org.springframework.stereotype.Service;
import org.dromara.pe.domain.bo.TaskReservationBo;
import org.dromara.pe.domain.vo.TaskReservationVo;
import org.dromara.pe.domain.TaskReservation;
import org.dromara.pe.mapper.TaskReservationMapper;
import org.dromara.pe.service.ITaskReservationService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 预约审核名单Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@RequiredArgsConstructor
@Service
public class TaskReservationServiceImpl implements ITaskReservationService {

    private final TaskReservationMapper baseMapper;


    /**
     * 查询预约审核名单
     */
    @Override
    public TaskReservationVo queryById(Long reservationId){
        return baseMapper.selectVoById(reservationId);
    }

    /**
     * 查询预约审核名单列表
     */
    @Override
    public TableDataInfo<TaskReservationVo> queryPageList(TaskReservationBo bo, PageQuery pageQuery) {
        QueryWrapper<TaskReservation> lqw = buildQueryWrapper(bo);
        Page<TaskReservationVo> result = baseMapper.selectTaskReservationList(pageQuery.build(),lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询预约审核名单列表
     */
    @Override
    public List<TaskReservationVo> queryList(TaskReservationBo bo) {
        QueryWrapper<TaskReservation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectTaskReservationList(lqw);
    }

    private QueryWrapper<TaskReservation> buildQueryWrapper(TaskReservationBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<TaskReservation> lqw = new QueryWrapper<>();
        lqw.eq(bo.getTaskId() != null, "peti.task_id", bo.getTaskId());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), "peti.task_name", bo.getTaskName());
        lqw.eq(bo.getArrangeId() != null, "petai.arrange_id", bo.getArrangeId());
        lqw.like(StringUtils.isNotBlank(bo.getArrangeName()), "petai.arrange_name", bo.getArrangeName());
        lqw.eq(StringUtils.isNotBlank(bo.getStuNum()), "pmi.id_number", bo.getStuNum());
        lqw.like(StringUtils.isNotBlank(bo.getStudentName()), "pmi.name", bo.getStudentName());
        lqw.eq(StringUtils.isNotBlank(bo.getYears()), "pmi.years", bo.getYears());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), "pmi.gender", bo.getGender());
        lqw.eq(bo.getClassId()!=null, "pmi.class_id", bo.getClassId());
        lqw.eq(StringUtils.isNotBlank(bo.getGrade()), "pbtc.grade", bo.getGrade());
        lqw.eq(StringUtils.isNotBlank(bo.getProfessionalId()), "sd1.dept_id", bo.getProfessionalId());
        lqw.eq(StringUtils.isNotBlank(bo.getCollegeId()), "sd2.dept_id", bo.getCollegeId());
        return lqw;
    }

    /**
     * 新增预约审核名单
     */
    @Override
    public Boolean insertByBo(TaskReservationBo bo) {
        TaskReservation add = MapstructUtils.convert(bo, TaskReservation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReservationId(add.getReservationId());
        }
        return flag;
    }

    /**
     * 修改预约审核名单
     */
    @Override
    public Boolean updateByBo(TaskReservationBo bo) {
        TaskReservation update = MapstructUtils.convert(bo, TaskReservation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskReservation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除预约审核名单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    public Boolean batchDelete(TaskReservationBo bo) {
        QueryWrapper<TaskReservation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(bo.getTaskId()!=null,"task_id",bo.getTaskId());
        queryWrapper.eq(bo.getArrangeId()!=null,"arrange_id",bo.getArrangeId());
        return baseMapper.delete(queryWrapper)>0;
    }
}
